package com.example.fengyungo.activity.main;

import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.fengyungo.R;
import com.example.fengyungo.activity.BaseActivity;
import com.example.fengyungo.adapter.ViewPagerAdapter;
import com.example.fengyungo.service.CommonIntentService;
import com.example.fengyungo.service.WebSocketService;
import com.google.android.material.tabs.TabLayout;

import java.util.Objects;

/**
 * @author XLR
 */
public class MainActivity extends BaseActivity {

    private static final String TAG = "MainActivity";

    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 登录成功后启动服务
        Log.d(TAG, "onCreate: serviceIntent");
        Intent serviceIntent = new Intent(this, WebSocketService.class);
        startService(serviceIntent);
        // 启动服务获取头像，将头像存入SharedPreferences
        Log.d(TAG, "onCreate: intentService");
        Intent intentService = new Intent(this, CommonIntentService.class);
        startService(intentService);

        //获取实例
        viewPager = findViewById(R.id.viewPager);
        tabLayout = findViewById(R.id.tabLayout);
        //设置适配器
        viewPagerAdapter = new ViewPagerAdapter(this,getSupportFragmentManager());
        viewPager.setAdapter(viewPagerAdapter);
        //将下面的TabLayout导航栏和viewpager进行关联
        tabLayout.setupWithViewPager(viewPager);

        //图标文字初始化设置
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            Log.d("ViewPager",i+"");
            Objects.requireNonNull(tabLayout.getTabAt(i)).setCustomView(viewPagerAdapter.getTabView(i));
        }

        //对TabLayout进行监听
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            ImageView imageView;
            //TabLayout选中时的操作
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    //根据位置，对图标进行设置
                    case 0:
                        imageView = tab.getCustomView().findViewById(R.id.TabIcon);
                        imageView.setImageResource(R.drawable.main_blue);
                        break;
                    case 1:
                        imageView = tab.getCustomView().findViewById(R.id.TabIcon);
                        imageView.setImageResource(R.drawable.message_blue);
                        break;
                    case 2:
                        imageView = tab.getCustomView().findViewById(R.id.TabIcon);
                        imageView.setImageResource(R.drawable.me_blue);
                        break;
                    default:
                        break;
                }
                Log.d("ViewPager",tab.getPosition()+"");
                //获取TabText，对文字的颜色进行设置
                TextView textView = tab.getCustomView().findViewById(R.id.TabText);
                textView.setTextColor(getResources().getColor(R.color.blue));
            }
            //TabLayout未选中时的操作
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    //根据位置，对图标进行设置
                    case 0:
                        imageView = tab.getCustomView().findViewById(R.id.TabIcon);
                        imageView.setImageResource(R.drawable.main_grey);
                        break;
                    case 1:
                        imageView = tab.getCustomView().findViewById(R.id.TabIcon);
                        imageView.setImageResource(R.drawable.message_grey);
                        break;
                    case 2:
                        imageView = tab.getCustomView().findViewById(R.id.TabIcon);
                        imageView.setImageResource(R.drawable.me_grey);
                        break;
                    default:
                        break;
                }
                //获取tabtext，对文字的颜色进行设置
                TextView textView = tab.getCustomView().findViewById(R.id.TabText);
                textView.setTextColor(getResources().getColor(R.color.gray));
            }
            //tablayout再次被选中时的操作
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
//                Toast.makeText(MainActivity.this,"You clicked again",Toast.LENGTH_SHORT).show();
            }
        });
    }
}